﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class ZL_CK_AddTest : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblTitle.Text = "新增仓库";
                initValue();
            }
        }

        private void initValue()
        {
            CS_CKLX_Dao daoCKLX = new CS_CKLX_Dao();
            List<CS_CKLX> lsCPLX = daoCKLX.QueryAll();
            this.ddlCKLXBH.DataSource = lsCPLX;
            this.ddlCKLXBH.DataValueField = "BH";
            this.ddlCKLXBH.DataTextField = "MC";
            this.ddlCKLXBH.DataBind();
            this.ddlCKLXBH.Items.Insert(0, new ListItem("请选择", "0"));
        }

   

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
            this.tbBH.Text = "";
            this.tbMC.Text = "";
            this.tbWZ.Text = "";
            this.ddlCKLXBH.SelectedValue = "0";
        }

        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";
            if (this.ddlCKLXBH.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择仓库类型";
                return;
            }
            ZL_CK entity = new ZL_CK()
            {
                ID = Guid.NewGuid(),
                BH = this.tbBH.Text.Trim(),
                MC = this.tbMC.Text.Trim(),
                WZ = this.tbWZ.Text.Trim(),
                CKLXBH = this.ddlCKLXBH.SelectedValue
            };

            string resultMessage = "";
            try
            {
                ZL_CK_Dao dao = new ZL_CK_Dao();

                if (dao.Exist(entity.BH, entity.MC))
                {
                    resultMessage = "同样编号或名称的仓库已存在";
                    return;
                }

                int result = dao.Add(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/ZL_CK_Query.aspx");
        }
    }
}